document.addEventListener("DOMContentLoaded", () => {
  const submitBtn = document.getElementById("submitBtn"); // 注册/登录按钮
  const findJobBtn = document.getElementById("findJob");
  const hireBtn = document.getElementById("hire");
  const termsCheckbox = document.getElementById("terms"); // 获取同意框的元素
  const errorMessage = document.getElementById("error-message"); // 错误信息显示区域

  findJobBtn.addEventListener("click", () => {
    findJobBtn.classList.add("active");
    hireBtn.classList.remove("active");
  });

  hireBtn.addEventListener("click", () => {
    hireBtn.classList.add("active");
    findJobBtn.classList.remove("active");
  });

  submitBtn.addEventListener("click", () => {
    const phone = document.getElementById("phone").value;
    const password = document.getElementById("verificationCode").value;
    const userType = findJobBtn.classList.contains("active") ? "jobSeeker" : "employer";

    errorMessage.style.display = "none";
    errorMessage.textContent = "";

    if (password.length <= 6) {
      errorMessage.textContent = "密码必须大于六位";
      errorMessage.style.display = "inline";
      return;
    }

    if (!termsCheckbox.checked) {
      errorMessage.textContent = "请勾选同意用户协议和隐私政策";
      errorMessage.style.display = "inline";
      return;
    }

    const userData = {
      phone,
      password,
      userType,
    };

    localStorage.setItem("userData", JSON.stringify(userData));

    // window.location.href = "../index.html";
  });

  const userData = localStorage.getItem("userData");
  if (userData) {
    const user = JSON.parse(userData);
    if (user.userType === "jobSeeker") {
      findJobBtn.classList.add("active");
      hireBtn.classList.remove("active");
    } else {
      hireBtn.classList.add("active");
      findJobBtn.classList.remove("active");
    }
  }
});
